package com.example.battle.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.battle.R;
import com.example.battle.db.BattleDao;
import com.example.battle.entity.FansName;
import com.example.battle.ui.adapter.SearchFansItemAdapter;
import com.example.battle.util.ActivityCollector;

import java.util.ArrayList;
import java.util.List;

public class FansNameSearchActivity extends AppCompatActivity  {
    private RecyclerView recyclerView;
    private RecyclerView.Adapter dataItemAdapter;
    private RecyclerView.LayoutManager layoutManager;
    private List<FansName> dataItemList = new ArrayList<>();
    private List<FansName> dataList = new ArrayList<>();
    private BattleDao battleDao;
    private Button button;
    private ImageView fansnameSearchBack;
    private SearchView searchView;

    @SuppressLint("RestrictedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fans_name_search);
//        要展示的数据
        recyclerView = findViewById(R.id.recyclerView);

//        让一条条数据线性排列
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        searchView = findViewById(R.id.fansname_serachview);
        button = findViewById(R.id.button_register_newfansname);
//        获得传过来的粉丝名
        Intent intent = getIntent();
        String fansName = intent.getStringExtra("fansName");
        searchView.setQuery(fansName, false);
//        搜索相似的名字作为各个item的数据
        searchFansNameItems(fansName);
        dataItemAdapter = new SearchFansItemAdapter(dataItemList);
        recyclerView.setAdapter(dataItemAdapter);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                打开注册粉丝名的界面
                Intent intent1 = new Intent(FansNameSearchActivity.this, RegisterFansNameActivity.class);
                startActivity(intent1);
            }
        });

        fansnameSearchBack = findViewById(R.id.fansname_search_back);
        fansnameSearchBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        searchView.setIconified(false);
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                searchFansNameItems(query);
                int position;
                dataItemAdapter.notifyDataSetChanged();
                if (dataItemList.size() == 0) {
                    Toast.makeText(FansNameSearchActivity.this, "该粉丝名未注册", Toast.LENGTH_SHORT).show();
                    return true;
                }
                for (FansName item : dataItemList) {
                    if (item.getFansName().equals(query)) {
                        position = dataItemList.indexOf(item);
                        recyclerView.scrollToPosition(position);
                        return true;
                    }
                }
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                searchFansNameItems(newText);
                int position;
                dataItemAdapter.notifyDataSetChanged();
                if (dataItemList.size() == 0) {
//                    recyclerView.removeAllViews();
                    Toast.makeText(FansNameSearchActivity.this, "该粉丝名未注册", Toast.LENGTH_SHORT).show();
                    return true;
                }
                for (FansName item : dataItemList) {
                    if (item.getFansName().equals(newText)) {
                        position = dataItemList.indexOf(item);
                        recyclerView.scrollToPosition(position);
                        return true;
                    }else {
                        button.setVisibility(View.VISIBLE);
                    }
                }
                return true;
            }
        });
    }


    public void searchFansNameItems(String fansName) {
        dataItemList.clear();
        battleDao = Login.getInstance().getAppDB().battleDao();
        dataList = battleDao.searchFansNameItems(fansName);
        for (int i = 0; i < dataList.size(); i++){
            dataItemList.add(dataList.get(i));
        }
        if (dataItemList.size() == 0){
            button.setVisibility(View.VISIBLE);
            Toast.makeText(FansNameSearchActivity.this, "该粉丝名未注册", Toast.LENGTH_SHORT).show();
        }else {
            button.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollector.removeActivity(this);
    }
}
